/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.compounder;

/**
 *
 * @author Mr.Sam
 */
import java.util.*;
import java.io.*;
import fr.inria.openmath.omapi.*;
import fr.inria.openmath.omapi.implementation.*;
import fr.ove.openmath.OpenMathizable;
import fr.ove.openmath.compounder.OMCompounderParserHandler;

/**
* An OpenMath objects compounder.<BR>
* The elements to compound are identified by name. These elements are compounded
* thanks to a "pattern" OpenMath object (the source) where the different elements
* are identified by means of variables whose values are the identifiers of the elements.
* <P>
* For example, lets assume the source is the following OpenMath object :
* <OMOBJ>
* <OMA>
* <OMS cd="arith1" name="plus"/>
* <OMV name="el1"/>
* <OMV name="el2"/>
* </OMA>
* </OMOBJ>
* with the element <OMOBJ><OMI>1</OMI></OMOBJ> identified by "el1" and
* the element <OMOBJ><OMI>2</OMI></OMOBJ> identified by "el2".<BR>
* The compounded OpenMath object is :
* <OMOBJ>
* <OMA>
* <OMS cd="arith1" name="plus"/>
* <OMI>1</OMI>
* <OMI>2</OMI>
* </OMA>
* </OMOBJ>
*
* @author © 2000 DIRAT Laurent
* @version 1.0 11/11/2000
*/
public class OMCompounder {
    /**
    * The OpenMath object source.
    */
    private String source;

    /**
    * The compounded OpenMath object.
    */
    private String compounded;

    /**
    * The list of the elements to compose
    */
    private Hashtable elements;

    /**
    * The parser handler.
    */
    private OMCompounderParserHandler parserHandler;

    /**
    * The parser
    */
    private Parser parser;

    public OMCompounder() {
        elements = new Hashtable();
        parserHandler = new OMCompounderParserHandler(elements);
        parser = new XMLParser(parserHandler);
    }

    /**
    * Adds an element to compound.
    * @param id the identifier of the element.
    * @param elem the element.
    */
    public void addElement(String id, OpenMathizable elem) {
        elements.put(id, elem);
    }

    /**
    * Removes the element with the specified identifier.
    * @param id the identifier of the element.
    */
    public void removeElement(String id) {
        elements.remove(id);
    }

    /**
    * Gets the compounded OpenMath object.
    */
    public String getCompounded() {
        return compounded;
    }

    /**
    * Builds the compounded OpenMath object.
    */
    public void compound() {
        if ((source != null) && (source.startsWith("<OMOBJ>"))) {
    	    ByteArrayInputStream in = new ByteArrayInputStream(source.getBytes());

            try {
                parser.initParse(in);
                // Setup done, now call the parser main loop.
                parser.parseObject(in);
                compounded = parserHandler.getCompounded();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
    * Sets the source OpenMath object.
    * @param source the source.
    */
    public void setSource(String source) {
        this.source = source;
    }

    /**
    * Returns thesource OpenMath object.
    */
    public String getSource() {
        return source;
    }
}